<template>
  <div>
    <el-row :gutter="10">
      <el-col :span="6">
        <el-button icon="el-icon-plus" type="primary" @click="addDialogVisible = true">
          添加分类
        </el-button>
      </el-col>
    </el-row>
    <!--    分类列表-->
    <el-table :data="categoryList">
      <el-table-column label="序号" type="index" width="50"></el-table-column>
      <el-table-column label="名称" prop="name"></el-table-column>
      <el-table-column label="操作">
        <template v-slot="scope">
          <el-button
            icon="el-icon-edit"
            size="mini"
            type="primary"
            @click="showEditDialog(scope.row)"
          >
            编辑
          </el-button>
          <el-popconfirm
            icon="el-icon-delete"
            iconColor="red"
            title="确定删除吗？"
            @onConfirm="deleteCategoryById(scope.row.id)"
          >
            <el-button slot="reference" icon="el-icon-delete" size="mini" type="danger">
              删除
            </el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <!--分页-->
    <el-pagination
      :current-page="queryBody.pageNum"
      :page-size="queryBody.pageSize"
      :page-sizes="[10, 20, 30, 50]"
      :total="total"
      background
      hide-on-single-page
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="handlePageSizeChange"
      @current-change="handleCurrentNumChange"
    ></el-pagination>

    <!--添加分类对话框-->
    <el-dialog
      :close-on-click-modal="false"
      :visible.sync="addDialogVisible"
      title="添加分类"
      width="50%"
      @close="closeAddDialog"
    >
      <!--内容主体-->
      <el-form ref="addFormRef" :model="addForm" :rules="formRules" label-width="80px">
        <el-form-item label="分类名称" prop="name">
          <el-input v-model="addForm.name"></el-input>
        </el-form-item>
      </el-form>
      <!--底部-->
      <span slot="footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCategory">确 定</el-button>
      </span>
    </el-dialog>

    <!--编辑分类对话框-->
    <el-dialog
      :close-on-click-modal="false"
      :visible.sync="editDialogVisible"
      title="编辑分类"
      width="50%"
      @close="closeEditDialog"
    >
      <!--内容主体-->
      <el-form ref="editFormRef" :model="editForm" :rules="formRules" label-width="80px">
        <el-form-item label="分类名称" prop="name">
          <el-input v-model="editForm.name"></el-input>
        </el-form-item>
      </el-form>
      <!--底部-->
      <span slot="footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editCategory">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import { addCategory, deleteCategoryById, editCategory, getData } from '@/api/category'
  import _ from 'lodash'

  export default {
    name: 'CategoryList',
    components: {},
    data() {
      return {
        queryBody: {
          pageNum: 1,
          pageSize: 10,
        },
        categoryList: [],
        total: 0,
        addDialogVisible: false,
        editDialogVisible: false,
        addForm: {
          name: '',
        },
        editForm: {},
        formRules: {
          name: [{ required: true, message: '请输入分类名称', trigger: 'blur' }],
        },
      }
    },
    computed: {},
    created() {
      this.fetchData()
    },
    methods: {
      fetchData() {
        getData(this.queryBody).then((res) => {
          this.categoryList = res.data.list
          this.total = res.data.total
        })
      },
      showEditDialog(row) {
        this.editForm = _.cloneDeep(row)
        this.editDialogVisible = true
      },
      deleteCategoryById(id) {
        deleteCategoryById(id).then((res) => {
          this.msgSuccess(res.msg)
          this.fetchData()
        })
      },
      handlePageSizeChange(newPageSize) {
        this.queryBody.pageSize = newPageSize
        this.fetchData()
      },
      handleCurrentNumChange(newCurrentNum) {
        this.queryBody.pageNum = newCurrentNum
        this.fetchData()
      },
      closeAddDialog() {
        this.$refs.addFormRef.resetFields()
      },
      addCategory() {
        this.$refs.addFormRef.validate((valid) => {
          if (valid) {
            addCategory(this.addForm).then((res) => {
              this.msgSuccess(res.msg)
              this.addDialogVisible = false
              this.fetchData()
            })
          }
        })
      },
      closeEditDialog() {
        this.editForm = {}
        this.$refs.editFormRef.resetFields()
      },
      editCategory() {
        this.$refs.editFormRef.validate((valid) => {
          if (valid) {
            editCategory(this.editForm).then((res) => {
              this.msgSuccess(res.msg)
              this.editDialogVisible = false
              this.fetchData()
            })
          }
        })
      },
    },
  }
</script>

<style lang="scss" scoped>
  .el-button + span {
    margin-left: 10px;
  }
</style>
